package com.qi.spzx.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.qi.spzx.manager.service.ValidateCodeService;
import com.qi.spzx.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2024/2/21 15:31
 * @Version 1.0.0
 */
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ValidateCodeVo generateValidateCode() {
        //通过工具生成图片验证码
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 2);
        //生成的验证码的值
        String code = circleCaptcha.getCode();
        //返回的验证码
        String imageBase64 = circleCaptcha.getImageBase64();
        //把验证码存储到redis中，设置value和key,设置过期时间
        String key = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set("user:validate"+key,code,5, TimeUnit.MINUTES );
        //创建vo对象，并进行赋值
        ValidateCodeVo validateCodeVo = new ValidateCodeVo();
        validateCodeVo.setCodeKey(key);
        validateCodeVo.setCodeValue("data:image/png;base64," + imageBase64);
        //进行返回
        return validateCodeVo;
    }

}
